package pers.liyan.shi.springcloudkafkatest.kafka;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import pers.liyan.shi.springcloudkafkatest.domain.UserDTO;

@Component
@Slf4j
public class ProducerTest {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    private ObjectMapper objectMapper = new ObjectMapper();

    public void sendUserMsg(UserDTO user) {
        try {
            kafkaTemplate.send("Hello-Kafka", String.valueOf(user.getId()), objectMapper.writeValueAsString(user));
            log.debug("send msg {} : {}", String.valueOf(user.getId()), objectMapper.writeValueAsString(user));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

}
